home *** CD-ROM | disk | FTP | other *** search
/ SGI Cosmo Software 1997 May / SGI Cosmo Software 1997 May.iso / dist6.2 / netscape.idb / usr / local / lib / netscape / conv_alias.z / conv_alias
Text File  |  1997-05-23  |  9KB  |  236 lines

  1. #!/bin/sh
  2.  
  3.  
  4. # This shell script reads $HOME/.zmailrc and converts aliases found to
  5. # an html file understood by Netscape's Address Book (version 2.x and
  6. # above).
  7. #     07/11/96 A.T. - Created.
  8. #     09/06/96 A.T. - Fixed to correctly parse email addresses in the
  9. #                     format of "john@sgi.com (John Smith)".
  10.  
  11.  
  12. PATH=/usr/sbin:/usr/bsd:/sbin:/usr/bin:/bin:/usr/bin/X11
  13. TDIR=/usr/tmp
  14. ZMAILRC=$HOME/.zmailrc
  15. ADDR_BOOK=$HOME/.netscape/address-book.html
  16. ADDR_BOOK_USER=$TDIR/.addr_book_user.$$
  17. ADDR_BOOK_LIST=$TDIR/.addr_book_list.$$
  18. MMAIL_ALIAS=$TDIR/.mmail_alias.$$
  19. MMAIL_LIST=$TDIR/.mmail_list.$$
  20. MMAIL_LIST2=$TDIR/.mmail_list2.$$
  21. NEW_ADDR_BOOK=$TDIR/.new_addr_book.$$
  22. NEW_USER=$TDIR/.new_user.$$
  23. NEW_LIST=$TDIR/.new_list.$$
  24. FINAL=$TDIR/.final.$$
  25.  
  26.  
  27. # If there's no MediaMail .zmailrc file, there's nothing to do.
  28.  
  29. if [ ! -f "$ZMAILRC" ]; then
  30.     echo "$ZMAILRC file does not exist."
  31.     exit
  32. fi
  33.  
  34.  
  35. # Create $HOME/.netscape directory if one doesn't exist.
  36.  
  37. if [ ! -d "$HOME/.netscape" ]; then
  38.     mkdir $HOME/.netscape
  39. fi
  40.  
  41.  
  42. # This script should run when netscape is not running.  Check this.
  43.  
  44. if [ -l "$HOME/.netscape/lock" ]; then
  45.     echo "\n***IMPORTANT: This script should run when netscape is not running.\n***Please quit netscape and hit return to continue.\c"
  46.     read ANS
  47.     echo
  48. fi
  49.  
  50.  
  51. # Backup original address book.
  52. #
  53. # Netscape doesn't automatically assign alias ids to all users and lists.
  54. # Just in case there is a MediaMail alias that should reference existing
  55. # Netscape users/lists, the following will assign alias ids to users and
  56. # lists that don't have one.
  57. #
  58. # Build temporary files of users and lists from address book.
  59.  
  60. if [ -f "${ADDR_BOOK}" ]; then
  61.     if [ -f "${ADDR_BOOK}.original" ]; then
  62.         cp ${ADDR_BOOK} ${ADDR_BOOK}.$$
  63.     else
  64.         cp ${ADDR_BOOK} ${ADDR_BOOK}.original
  65.     fi
  66.  
  67.     nawk -F'"' 'BEGIN { AC=10000 }
  68.                { if ( $1 ~ "A HREF"  &&  $3 ~ "NICKNAME" )
  69.                     { AC=AC+1;
  70.                       printf ("%s\"%s\" ALIASID=\"%s\"%s\"%s\"%s\n", $1, $2, AC, $3, $4, $5);
  71.                     }
  72.                 else if ( $1 ~ "H3 "  &&  $0 !~ "ALIASID"  &&  $0 !~ "ALIASOF" )
  73.                     { AC=AC+1;
  74.                       printf ("    <DT><H3 ALIASID=\"%s\" NICKNAME=\"%s\"%s\n", AC, $2, $3);
  75.                     }
  76.                 else
  77.                     print $0;
  78.                }' < $ADDR_BOOK > $NEW_ADDR_BOOK
  79.  
  80.     sed -n 's/.*A HREF="\(.*\)" ALIASID\(.*\) NICKNAME="\(.*\)">\(.*\)<.*/\3\|\4|\1|\2/p' < $NEW_ADDR_BOOK > $ADDR_BOOK_USER
  81.     sed -n 's/.*H3 ALIASID\(.*\) NICKNAME="\(.*\)".*/\2|\1/p' < $NEW_ADDR_BOOK > $ADDR_BOOK_LIST
  82. else
  83.     touch $ADDR_BOOK_USER $ADDR_BOOK_LIST $NEW_ADDR_BOOK
  84. fi
  85.  
  86.  
  87. # Build temporary files of aliases and mailing lists from MediaMail's .zmailrc
  88. # file.
  89.  
  90. sed -n 's/^ *alias *\([^ ]*\) *\(.*\)/\1|\2/p' $ZMAILRC | grep -v "," | tr -d "'" | tr -d '"' > $MMAIL_ALIAS
  91. sed -n 's/^ *alias *\([^ ]*\) *\(.*\)/\1|\2/p' $ZMAILRC | grep "," | tr -d "'" | tr -d '"' > $MMAIL_LIST
  92.  
  93.  
  94. # Process MediaMail users file first.  If user is not already in address book,
  95. # add user to the user temp html file.  If address is a mailing list, skip it
  96. # for now by adding it to the lists file.  Netscape requires that nickname
  97. # contains only lowercase characters, numbers or "_", so there may be a
  98. # slight translation of the alias name.  If the address is in the format
  99. # of "John Smith <john@sgi.com>" or "john@sgi.com (John Smith)", then the
  100. # Name field will be John Smith.  Otherwise, it will be the same as the alias
  101. # name.
  102.  
  103. ALIAS_COUNT=20000
  104. while read LINE
  105. do
  106.     NICKNAME=`echo $LINE | cut -d"|" -f1 | tr '[A-Z]' '[a-z]' | tr "\!@#$%^&\*()-+={}\[\]|\:;<>,.?/ " "[_*]"`
  107.     NAME="$NICKNAME"
  108.     ADDR=`echo $LINE | cut -d"|" -f2`
  109.     if [ -z "`grep \"^${NICKNAME}\|\" $ADDR_BOOK_USER`" ]; then
  110.         if [ -z "`grep \"^${ADDR}\|\" $ADDR_BOOK_LIST $MMAIL_LIST`" ]; then
  111.             ALIAS_COUNT=`expr $ALIAS_COUNT + 1`
  112.             if [ -n "`echo $ADDR | grep '<'`" ]; then
  113.                 NAME=`echo $ADDR | sed -n 's/\(.*\) <\(.*\)>/\1/p'`
  114.                 ADDR=`echo $ADDR | sed -n 's/\(.*\) <\(.*\)>/\2/p'`
  115.                 if [ -z "$NAME" ]; then
  116.                     NAME="$NICKNAME"
  117.                 fi
  118.             elif [ -n "`echo $ADDR | grep '('`" ]; then
  119.                 NAME=`echo $ADDR | sed -n 's/\(.*\) (\(.*\))/\2/p'`
  120.                 ADDR=`echo $ADDR | sed -n 's/\(.*\) (\(.*\))/\1/p'`
  121.                 if [ -z "$NAME" ]; then
  122.                     NAME="$NICKNAME"
  123.                 fi
  124.             fi
  125.             echo "    <DT><A HREF=\"mailto:${ADDR}\" ALIASID=\"$ALIAS_COUNT\" NICKNAME=\"$NICKNAME\">$NAME</A>" >> $NEW_USER
  126.             echo "$NICKNAME|$NAME|mailto:$ADDR|=\"$ALIAS_COUNT\"" >> $ADDR_BOOK_USER
  127.         else
  128.             echo $LINE >> $MMAIL_LIST
  129.         fi
  130.     else
  131.         echo "Alias $NICKNAME not added because it exists in the Address Book already."
  132.     fi
  133. done < $MMAIL_ALIAS
  134.  
  135.  
  136. # Assign alias ids to the MediaMail lists file. Have to do this first because
  137. # lists can reference other lists.
  138.  
  139. nawk 'BEGIN { AC=30000 }
  140.      { AC=AC+1;
  141.        printf ("%s|=\"%s\"\n", $0, AC);
  142.      }' < $MMAIL_LIST > $MMAIL_LIST2
  143.  
  144.  
  145. # Process MediaMail lists file now.  If list is not already in address book,
  146. # get the alias id for the list and add list to the list temp html file.
  147. # For each address in the list, determine whether it is a user or list.
  148. # If user/list is not already in address book, add user/list to the appropriate
  149. # temp html file.  If user/list is in the address book, find its alias id
  150. # and reference it in the list temp html file.
  151.  
  152. ALIAS_COUNT=40000
  153. while read LINE
  154. do
  155.     ORIG_NICKNAME=`echo $LINE | cut -d"|" -f1`
  156.     NICKNAME=`echo $LINE | cut -d"|" -f1 | tr '[A-Z]' '[a-z]' | tr "\!@#$%^&\*()-+={}\[\]|\:;<>,.?/ " "[_*]"`
  157.  
  158.     if [ -z "`grep \"^${NICKNAME}\|\" $ADDR_BOOK_LIST`" ]; then
  159.         ALIASID=`grep "^${ORIG_NICKNAME}\|" $MMAIL_LIST2 | cut -d"|" -f3`
  160.         echo "    <DT><H3 ALIASID$ALIASID NICKNAME=\"$NICKNAME\">$NICKNAME</H3>" >> $NEW_LIST
  161.         echo "    <DL><p>" >> $NEW_LIST
  162.         echo "$NICKNAME|$ALIASID" >> $ADDR_BOOK_LIST
  163.  
  164.         NBR_ADDR=`echo $LINE | awk -F, '{print NF}'`
  165.         ALL_ADDR=`echo $LINE | cut -d"|" -f2`
  166.  
  167.         ADDR_INDEX=1
  168.         while [ $ADDR_INDEX -le $NBR_ADDR ]
  169.         do
  170.             ADDR=`echo $ALL_ADDR | cut -d, -f$ADDR_INDEX`
  171.             ADDR=`echo $ADDR`
  172.             NAME=""
  173.  
  174.             if [ -n "`echo $ADDR | grep '<'`" ]; then
  175.                 NAME=`echo $ADDR | sed -n 's/\(.*\) <\(.*\)>/\1/p'`
  176.                 ADDR=`echo $ADDR | sed -n 's/\(.*\) <\(.*\)>/\2/p'`
  177.             elif [ -n "`echo $ADDR | grep '('`" ]; then
  178.                 NAME=`echo $ADDR | sed -n 's/\(.*\) (\(.*\))/\2/p'`
  179.                 ADDR=`echo $ADDR | sed -n 's/\(.*\) (\(.*\))/\1/p'`
  180.             fi
  181.  
  182.             if [ -n "$NAME" ]; then
  183.                 NNAME=`echo $NAME | tr '[A-Z]' '[a-z]' | tr "\!@#$%^&\*()-+={}\[\]|\:;<>,.?/ " "[_*]"`
  184.             else
  185.                 NAME="$ADDR"
  186.                 NNAME=`echo $ADDR | tr '[A-Z]' '[a-z]' | tr "\!@#$%^&\*()-+={}\[\]|\:;<>,.?/ " "[_*]"`
  187.             fi
  188.  
  189.             if [ -z "`grep \"^${NNAME}\|\" $ADDR_BOOK_USER`" ]; then
  190.                 if [ -z "`grep \"^${NNAME}\|\" $ADDR_BOOK_LIST`"  -a  -z "`grep \"^${NAME}\|\" $MMAIL_LIST2`" ]; then
  191.                     ALIASOF=`grep "mailto:${ADDR}\|" $ADDR_BOOK_USER | cut -d"|" -f4`
  192.                     if [ -z "$ALIASOF" ]; then
  193.                         ALIAS_COUNT=`expr $ALIAS_COUNT + 1`
  194.                         echo "    <DT><A HREF=\"mailto:${ADDR}\" ALIASID=\"$ALIAS_COUNT\" NICKNAME=\"$NNAME\">$NAME</A>" >> $NEW_USER
  195.                         echo "$NNAME|$NAME|mailto:$ADDR|=\"$ALIAS_COUNT\"" >> $ADDR_BOOK_USER
  196.                         ALIASOF="=\"$ALIAS_COUNT\""
  197.                         echo "        <DT><A HREF=\"mailto:${ADDR}\" ALIASOF${ALIASOF}></A>" >> $NEW_LIST
  198.                     else
  199.                         echo "        <DT><A HREF=\"mailto:${ADDR}\" ALIASOF${ALIASOF}></A>" >> $NEW_LIST
  200.                     fi
  201.                 else
  202.                     ALIASOF=`grep "^${NNAME}\|" $ADDR_BOOK_LIST | cut -d"|" -f2`
  203.                     if [ -z "$ALIASOF" ]; then
  204.                         ALIASOF=`grep "^${NNAME}\|" $MMAIL_LIST2 | cut -d"|" -f3`
  205.                     fi
  206.                     echo "        <DT><H3 ALIASOF${ALIASOF}></H3>" >> $NEW_LIST
  207.                 fi
  208.             else
  209.                 ALIASOF=`grep "^${NNAME}\|" $ADDR_BOOK_USER | cut -d"|" -f4`
  210.                 echo "        <DT><A HREF=\"mailto:${ADDR}\" ALIASOF${ALIASOF}></A>" >> $NEW_LIST
  211.             fi
  212.  
  213.             ADDR_INDEX=`expr $ADDR_INDEX + 1`
  214.         done
  215.  
  216.         echo "    </DL><p>" >> $NEW_LIST
  217.     else
  218.         echo "Alias $NICKNAME not added because it exists in the Address Book already."
  219.     fi
  220. done < $MMAIL_LIST
  221.  
  222.  
  223. # Concatenate the new user and new list files to address book.
  224.  
  225. cat $NEW_ADDR_BOOK > $FINAL
  226. echo "<DL><p>" >> $FINAL
  227. cat $NEW_USER $NEW_LIST 2>/dev/null >> $FINAL
  228. echo "</DL><p>" >> $FINAL
  229. cp $FINAL $ADDR_BOOK
  230.  
  231.  
  232. # Clean up.
  233.  
  234. rm -f $ADDR_BOOK_USER $ADDR_BOOK_LIST $MMAIL_ALIAS $MMAIL_LIST $MMAIL_LIST2 $NEW_ADDR_BOOK $NEW_USER $NEW_LIST $FINAL 2>/dev/null
  235. echo "\nAlias conversion completed.\n"
  236.